﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evpo.ContactData.UI.GeneralClasses;
using Evpo.ContactData.CustomControls;

namespace Evpo.ContactData.UI
{
    public partial class ContactEditForm : System.Web.UI.Page, ILookupHost
    {
        public ContactEditForm()
        {
            _lookupControls = new Dictionary<string, ILookupControl>();
        }

        #region Fields
        private bool _contactEditVisible = true;
        private NameValueLookup _lookup;
        private Dictionary<string, ILookupControl> _lookupControls;
        #endregion

        #region Handlers
        protected void Page_PreInit(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            string lookupName = Request.Params[uiLookupToOpen.UniqueID];
            if (!String.IsNullOrEmpty(lookupName))
            {
                _contactEditVisible = false;
                _lookup = (NameValueLookup)Page.LoadControl("~/NameValueLookup.ascx");
                _lookup.LookupName = lookupName;
                _lookup.LookupHiddenID = uiLookupToOpen.ClientID;
                _lookup.SelectedItemsChanged += new SelectedItemsChangedEventHandler(Lookup_SelectedItemsChanged);
                _lookup.Multiselect = _lookupControls[lookupName].Multiselect;
                uiLookupPlaceHolder.Controls.Add(_lookup);
            }
            else
            {
                _contactEditVisible = true;
            }
        }

        void Lookup_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e)
        {
            uiContactEdit.UpdateEditableLookups();

            SelectedItemsEventArgs e1 = new SelectedItemsEventArgs()
            {
                LookupName = _lookup.LookupName,
                SelectedItems = e.SelectedItems
            };

            OnSetSelectedItems(e1);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (uiContactEdit.Visible != _contactEditVisible)
            {
                uiContactEdit.UpdateEditableLookups();

            }

            if (!_contactEditVisible)
            {
                _lookup.SelectedItems = GetSelectedItemsForLookup(_lookup.LookupName);
            }

            uiContactEdit.Visible = _contactEditVisible;
        }

        #endregion Handlers

        #region Events
        public event SelectedItemsEventHandler GetSelectedItems;
        public void OnGetSelectedItems(SelectedItemsEventArgs e)
        {
            if (GetSelectedItems != null)
                GetSelectedItems(this, e);
        }

        public event SelectedItemsEventHandler SetSelectedItems;
        public void OnSetSelectedItems(SelectedItemsEventArgs e)
        {
            if (SetSelectedItems != null)
                SetSelectedItems(this, e);
        }

        #endregion Events

        #region Methods

        public void RegisterLookupControl(ILookupControl cnt)
        {
            _lookupControls.Add(cnt.LookupName, cnt);
        }

        public List<int> GetSelectedItemsForLookup(string lookupName)
        {
            SelectedItemsEventArgs e = new SelectedItemsEventArgs()
            {
                LookupName = lookupName
            };

            OnGetSelectedItems(e);

            return e.SelectedItems;
        }

        #endregion Methods

    }
}